package com.vincent.seeyou.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.vincent.seeyou.R;
import com.vincent.seeyou.model.BmobConstants;
import com.vincent.seeyou.model.User;
import com.vincent.seeyou.util.CommonUtils;

import cn.bmob.im.util.BmobLog;
import cn.bmob.v3.BmobInstallation;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by vincent on 14-9-1.
 */
public class RegisterActivity extends BaseActivity {

    private EditText editText_name,editText_password,editText_comfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        editText_name = (EditText)findViewById(R.id.editText_name);
        editText_password = (EditText)findViewById(R.id.editText_password);
        editText_comfirm = (EditText)findViewById(R.id.editText_comfirm_password);
    }

    public void onSubmit(View view) {
        register();
    }

    private void register(){
        String name = editText_name.getText().toString();
        String password = editText_password.getText().toString();
        String pwd_again = editText_password.getText().toString();

        if (TextUtils.isEmpty(name)) {
            ShowToast(R.string.tip_error_username);
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ShowToast(R.string.tip_error_password);
            return;
        }
        if (!pwd_again.equals(password)) {
            ShowToast(R.string.tip_error_comfirm_password);
            return;
        }

        boolean isNetConnected = CommonUtils.isNetworkAvailable(this);
        if(!isNetConnected){
            ShowToast(R.string.tip_network);
            return;
        }

        final ProgressDialog progress = new ProgressDialog(RegisterActivity.this);
        progress.setMessage(getString(R.string.tip_is_registering));
        progress.setCanceledOnTouchOutside(false);
        progress.show();
        //由于每个应用的注册所需的资料都不一样，故IM sdk未提供注册方法，用户可按照bmod SDK的注册方式进行注册。
        //注册的时候需要注意两点：1、User表中绑定设备id和type，2、设备表中绑定username字段
        final User bu = new User();
        bu.setUsername(name);
        bu.setPassword(password);
        //将user和设备id进行绑定
        bu.setDeviceType(BmobConstants.DEVICE_TYPE);
        bu.setInstallId(BmobInstallation.getInstallationId(this));
        bu.signUp(RegisterActivity.this, new SaveListener() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub
                progress.dismiss();
                ShowToast(getString(R.string.tip_success_register));
                // 将设备与username进行绑定
                bmobUserManager.bindInstallationForRegister(bu.getUsername());
                //更新地理位置信息
                updateUserLocation();

                //发广播通知登陆页面退出
                sendBroadcast(new Intent(BmobConstants.ACTION_REGISTER_SUCCESS_FINISH));
                // 启动主页
                Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                startActivity(intent);
                finish();

            }

            @Override
            public void onFailure(int arg0, String arg1) {
                // TODO Auto-generated method stub
                BmobLog.i(arg1);
                ShowToast(getString(R.string.tip_failed_register)+ arg1);
                progress.dismiss();
            }
        });
    }
}
